package com.alan.netty.netty.bytebuf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.Unpooled;
import io.netty.buffer.UnpooledByteBufAllocator;

/**
 * @author weiBin
 * @date 2018/11/23
 */
public class ByteBufCreate {
    public static void main(String[] args) {
        PooledByteBufAllocator allocator = new PooledByteBufAllocator();
        ByteBuf buffer1 = allocator.buffer();
        buffer1.writeBytes("I".getBytes());
        printByteBuf(buffer1);
        ByteBuf buffer2 = allocator.buffer(10);
        buffer2.writeBytes("Love".getBytes());
        printByteBuf(buffer2);
        ByteBuf buffer3 = allocator.directBuffer();
        buffer1.writeBytes("You".getBytes());
        printByteBuf(buffer3);
        ByteBuf buffer4 = allocator.directBuffer(10);
        buffer4.writeBytes("So".getBytes());
        printByteBuf(buffer4);
        ByteBuf buffer5 = allocator.heapBuffer();
        buffer1.writeBytes("Much".getBytes());
        printByteBuf(buffer5);
        ByteBuf buffer6 = allocator.heapBuffer(10);
        buffer4.writeBytes("!".getBytes());
        printByteBuf(buffer6);
        ByteBuf buffer7 = Unpooled.buffer();
        buffer4.writeBytes("h".getBytes());
        printByteBuf(buffer7);
    }

    public static void printByteBuf(ByteBuf byteBuf) {
        int readerIndex = byteBuf.readerIndex();
        int writerIndex = byteBuf.writerIndex();
        int capacity = byteBuf.capacity();
        boolean hasArray = byteBuf.hasArray();
        System.out.println(String.format("readerIndex=%s,writerIndex=%s,capacity=%s,hasArray=%s,ByteBuf=%s", readerIndex, writerIndex, capacity, hasArray, byteBuf.toString()));
    }
}
